package com.itheima.string;

public class StringDemo2 {
    public static void main(String[] args) {
        String s1 = "abc";
        System.out.println(s1.length());

        char c = s1.charAt(0);
        System.out.println(c);

        for (int i = 0; i <s1.length(); i++) {
            char ch = s1.charAt(i);
            System.out.print(ch);
        }

        System.out.println("------");

        char[] chars = s1.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]);
        }
        System.out.println();
        //
        String s2 = new String("黑阿米");
        String s3 = new String("黑阿米");
        System.out.println(s2 == s3);
        System.out.println(s2.equals(s3));// 比较内容是否相同

        // 比较字符串时，区分大小写
        String c1 = "abc";
        String c2 = "ABC";
        System.out.println(c1.equals(c2));
        System.out.println(c1.equalsIgnoreCase(c2));

        //截取字符串内容
        String s4 = "黑阿米";
        System.out.println(s4.substring(1));    // 从索引1开始截取到最后
        System.out.println(s4.substring(1,3));  // 从索引1开始截取到3

        //替换字符串内容
        String s5 = "黑阿米";
        System.out.println(s5.replace("黑","白"));

        //判断某个字符串是否包含某个关键字
        String s6 = "黑阿米";
        System.out.println(s6.contains("黑"));

        //判断字符串是否以某个字符开头
        System.out.println(s6.startsWith("黑"));
        //判断字符串是否以某个字符结尾
        System.out.println(s6.endsWith("米"));

        //把字符串按照某个字符分割成多个字符串，并存放在数组中返回给我们
        String s7 = "黑,阿,米";
        String[] split = s7.split(",");
        for (int i = 0; i < split.length; i++) {
            System.out.print(split[i]);
        }
    }
}
